package parser;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import log.Logger;

import net.rim.device.api.xml.parsers.DocumentBuilderFactory;
import net.rim.device.api.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import screens.Controller;

public class DesPushParser {
	
	public boolean parseXML(String xml) {
		//System.out.println("****** xml"+xml);
		boolean suscripto = Controller.getInstance().getPushStatus();
		try {
			Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder()
			.parse(new ByteArrayInputStream(xml.getBytes("ISO-8859-1")));
			doc.getDocumentElement().normalize();
			NodeList list = doc.getElementsByTagName("*");
			// Suscripto: true
			// Estado: Ok ??
			String node = new String();
			String element = new String();
			for (int i = 0; i < list.getLength(); i++) {
				if (list.item(i).hasChildNodes()){
					Node value = list.item(i).getChildNodes().item(0);
					node = list.item(i).getNodeName();
					element = value.getNodeValue();

					System.out.println("******************************************"+node+": "+element);

					if (node.equals("Estado")){
						if (element.equals("0")){
							Logger.log("parsing: suscripto=false");
							suscripto=false;
						} 
					}
				}
			}
			System.out.println("****** Suscripto: "+suscripto);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			Logger.log("dessuscribir push parser error");
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
			Logger.log("dessuscribir push parser error");
		} catch (SAXException e) {
			e.printStackTrace();
			Logger.log("dessuscribir push parser error");
		} catch (IOException e) {
			e.printStackTrace();
			Logger.log("dessuscribir push parser error");
		}
		return suscripto;

	}


}
